<!DOCTYPE html>
<html lang="en-us" id="extr-page">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" media="screen" href="../../global/css/bootstrap.min.css">
    <link rel="stylesheet" href="../../global/css/ztree/zTreeStyle/zTreeStyle.css" type="text/css">
    <link rel="stylesheet" href="../../global/css/ztree/demo.css" type="text/css">
</head>
<body>
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
    <form class="form-horizontal" onsubmit="return false" id="form">
        <fieldset>
            <input type="hidden" id="id" name="id">

            <div class="form-group">
                <label class="col-md-2 control-label">上级分类:</label>
                <div class="col-md-10" style="width: 160px">
                    <select class="form-control input-sm" name="pid" id="pid">
                        <option value="">root</option>
                    </select>
                </div>
            </div>

            <div class="form-group">
                <label class="col-md-2 control-label">名称</label>
                <div class="col-md-10">
                    <input class="form-control" placeholder="名称" type="text" name="name" id="name"
                           data-bv-notempty="true"
                           data-bv-notempty-message="名称 不能为空">
                </div>
            </div>

            <div class="form-group">
                <label class="col-md-2 control-label">所属店铺:</label>
                <div class="col-md-10" style="width: 160px">
                    <select class="form-control input-sm" name="shopId" id="shopsId">
                    </select>
                </div>
            </div>

            <div class="form-group">
                <label class="col-md-2 control-label">排序</label>
                <div class="col-md-10">
                    <input class="form-control" placeholder="排序" name="typeSort" id="typeSort"  value="00001"
                           data-bv-notempty="true"
                           data-bv-notempty-message="排序 不能为空">
                </div>
            </div>

            <div class="form-actions">
                <div class="row" align="center">
                    <div class="col-md-12">
                        <button class="btn btn-primary" onclick="location.href='goodstypeList.html'">返回</button>
                        <button class="btn btn-primary" type="submit" onclick="add()">
                            <i class="fa fa-save"></i> 保存
                        </button>
                    </div>
                </div>
            </div>
        </fieldset>
    </form>
</div>

<script type="text/javascript" src="../../global/js/libs/jquery-2.1.1.min.js"></script>
<script type="text/javascript" src="../../global/js/jq.js"></script>
<script type="text/javascript" src="../../global/js/plugin/bootstrapvalidator/bootstrapValidator.min.js"></script>
<script type="text/javascript" src="../../global/js/common.js"></script>
<script type="text/javascript" src="../../global/layui/layui.js"></script>
<script type="text/javascript" src="../../global/js/libs/jquery.ztree.all-3.5.min.js"></script>
<script type="text/javascript" src="../../global/js/my/ztree-menu.js"></script>
<script type="text/javascript">
    $(function () {
        $.ajax({
            url: "/clyjNearbyShops/getshops",
            dataType: 'json',
            type: 'post',
            async: false,
            success: function (data) {
                $.each(data, function (index, data) {
                    $("#shopsId").append("<option value='" + data.shopsId + "'>" + data.shopsName + "</option>");
                });
            }
        })
        $.ajax({
            url: "/clyjNearbyGoodsType/getType",
            dataType: 'json',
            type: 'post',
            async: false,
            success: function (data) {
                $.each(data, function (index, data) {
                    $("#pid").append("<option value='" + data.id + "'>" + data.name + "</option>");
                });
            }
        })
        layui.use('layer', function () {
            var layer = layui.layer;
        });

        initData();
    })

    function initData() {
        var id = getUrlParam("id");
        if (id != "") {
            $.ajax({
                type: 'get',
                url: '/clyjNearbyGoodsType/clyshopsList' + id,
                async: false,
                success: function (result) {
                    var data = result.data;
                    $("#id").val(data.id);
                    $("#name").val(data.name);
                }
            });

        }
    }
    $('#form').bootstrapValidator();

    function add() {
        var bootstrapValidator = $("#form").data('bootstrapValidator');
        bootstrapValidator.validate();
        if (!bootstrapValidator.isValid()) {
            return;
        }

        var formdata = $("#form").serializeObject();

        $.ajax({
            type: 'post',
            url: '/clyjNearbyGoodsType/saveType',
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify(formdata),
            success: function (data) {
                layer.msg("成功", {shift: -1, time: 1000}, function () {
                    location.href = "goodstypeList.html";
                });
            }
        });
    }

</script>
</body>
</html>